package com.leyou.personnel.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leyou.common.pojo.R;
import com.leyou.common.pojo.Result;
import com.leyou.annotation.AuthorityForResultCheck;
import com.leyou.personnel.data.entity.AdministratorsRole;
import com.leyou.personnel.data.entity.AdministratorsRoleAuthority;
import com.leyou.personnel.data.vo.AuthoritySaveUpdateVo;
import com.leyou.personnel.mapper.AdministratorsRoleAuthorityMapper;
import com.leyou.personnel.service.AdministratorsAuthorityService;
import com.leyou.personnel.service.AdministratorsRoleAuthorityService;
import com.leyou.personnel.service.AdministratorsRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xhh
 * @since 2022-05-19
 */
@RestController
@RequestMapping("/leyou.personnel/administrators-authority")
public class AdministratorsAuthorityController {
    @Autowired
    private AdministratorsRoleService administratorsRoleService;
    @Autowired
    private AdministratorsAuthorityService authorityService;
    @Autowired
    private AdministratorsRoleAuthorityService administratorsRoleAuthorityService;
    @Autowired
    private AdministratorsRoleAuthorityMapper administratorsRoleAuthorityMapper;
    /**
     * 权限列表
     *
     * @author: xhh
     * @date: 2022/5/26
     * @description:
     * @return:
     * @params:
     */
    @AuthorityForResultCheck(authority = "au009")
    @RequestMapping(value = "/authorityList",method = RequestMethod.POST)
    public Result authorityList(){
        return authorityService.authorityList();
    }
    /**
    * todo 修改角色回显
    * @param 
    * @return com.leyou.common.pojo.R
    * @author xhh
    * @data 2022/7/26 9:33
    * @deprecated 
    */
    @AuthorityForResultCheck(authority = "au011")
    @RequestMapping(value = "/authorityEcho",method = RequestMethod.POST)
    public R authorityEcho(@RequestParam("roleNumber") String roleNumber){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("role_number",roleNumber);
        queryWrapper.select("authority_number");
        List<AdministratorsRoleAuthority> list = administratorsRoleAuthorityService.list(queryWrapper);
        Set<String> stringSet=new HashSet<>();
        for (AdministratorsRoleAuthority administratorsRoleAuthority : list) {
            String authorityNumber = administratorsRoleAuthority.getAuthorityNumber();
            stringSet.add(authorityNumber);
        }
        return R.ok().data("list",stringSet);
    }
    /**
    * todo 权限修改
    * @param vo
    * @return com.leyou.common.pojo.R
    * @author xhh
    * @data 2022/7/26 11:18
    * @deprecated 1修改 role表 2并且删除角色和权限的关联表中的所有 为 roleNumber的字段，再重新添加
    */
    @Transactional
    @AuthorityForResultCheck(authority = "au011")
    @RequestMapping(value = "/authorityModified",method = RequestMethod.POST)
    public R authorityModified(@RequestBody AuthoritySaveUpdateVo vo){
        //1
        QueryWrapper<AdministratorsRole> queryWrapper=new QueryWrapper();
        queryWrapper.eq("role_number",vo.getRoleNumber());
        AdministratorsRole role=new AdministratorsRole();
        role.setRoleName(vo.getRoleName());
        boolean update = administratorsRoleService.update(role, queryWrapper);
        //2
        List<AdministratorsRoleAuthority> list = vo.getAuthorities();
        boolean b = administratorsRoleAuthorityMapper.realDeletes(vo.getRoleNumber());
        boolean saveBatch=false;
        if(b){
            saveBatch= administratorsRoleAuthorityService.saveBatch(list);
        }
        if(update && b && saveBatch){
            return R.ok();
        }else{
            return R.error();
        }
    }
    /**
    * todo 角色添加
    * @param vo
    * @return com.leyou.common.pojo.R
    * @author xhh
    * @data 2022/7/26 11:18
    * @deprecated  1 添加角色 2并赋予权限
    */
    @AuthorityForResultCheck(authority = "au010")
    @Transactional
    @RequestMapping(value = "/authoritySave",method = RequestMethod.POST)
    public R authoritySave(@RequestBody AuthoritySaveUpdateVo vo){
        System.err.println(vo);
        //1
        List<AdministratorsRole> list = administratorsRoleService.list();
        Integer max=0;
        for (AdministratorsRole role : list) {
            Integer newNumber = numberToString(role.getRoleNumber());
            if(newNumber>max){
                max=newNumber;
            }
        }
        max++;
        //获取到role编码
        String roleNumber = intToString(max);
        AdministratorsRole role=new AdministratorsRole();
        role.setRoleNumber(roleNumber);
        role.setRoleName(vo.getRoleName());
        boolean save = administratorsRoleService.save(role);
        List<AdministratorsRoleAuthority> authorities = vo.getAuthorities();
        List<AdministratorsRoleAuthority> authorityRoles =new ArrayList<>();

        for (AdministratorsRoleAuthority authority : authorities) {
            AdministratorsRoleAuthority roleAuthority =new AdministratorsRoleAuthority();
            roleAuthority.setAuthorityNumber(authority.getAuthorityNumber());
            roleAuthority.setRoleNumber(roleNumber);
            authorityRoles.add(roleAuthority);
        }
        boolean saveBatch = administratorsRoleAuthorityService.saveBatch(authorityRoles);
        if(saveBatch && save){
            return R.ok();
        }else{
            return R.error();
        }
    }
    //例如00102得到102
    public Integer numberToString(String roleNumber){
        char[] chars = roleNumber.toCharArray();
        StringBuffer stringBuffer=new StringBuffer("");
        int f=0;
        for (int i = 0; i <chars.length; i++) {
            if(chars[i]!='0'){
                f=1;
            }
            if(f==1){
                stringBuffer.append(chars[i]);
            }
        }
        return Integer.parseInt(stringBuffer.toString());
    }
    //例如102得到00102
    public String intToString(Integer roleNum){
        int i=5;
        int j=roleNum;
        int count=0;
        //计算位
        while(j>0){
            j=j/10;
            count++;
        }
        StringBuffer stringBuffer=new StringBuffer("");
        for (int i1 = 0; i1 < i; i1++) {
            if(i1+count<i){
                stringBuffer.append("0");
            }else{
                stringBuffer.append(roleNum.toString());
                break;
            }
        }
        String roleNumber = stringBuffer.toString();
        if(roleNumber.length()==i){
            return roleNumber;
        }else{
            return "";
        }

    }

}

